package ac.manchester.cs.afzal.autoservd.regular_expressions;

import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * Title: Term Classifier
 *
 * Description: A toolkit to find the relevance of candidate terms from a text corpus
 * to any particular topic/sub-domain using a term classification driven approach. The
 * system utilizes the lexical and contextual profiles of the candidate and domain-representing
 * "Resource Terms" (Seed and Ontological). The lexical and contextual profiles of candidate terms
 * are compared with those of Resource Terms to measure their relevance to the topic of interest.
 *
 * Derive_Distinct_Sentences class converts the
 * <p>Company: The University of Manchester</p>
 *
 * @author Hammad Afzal
 * @version 1.0

 */
public class String_Manipulator
{

    public String pattern_replacer(String str, String pat, String replacer)
    {
        Pattern pattern = Pattern.compile(pat);
        Matcher matcher = pattern.matcher(str);
        return matcher.replaceAll(replacer);
    }

    public String string_replacer(String str, String startPat, String endPat, String replacer)
    {
        int start = 0;
        int temp = 0;
        while((start = str.indexOf(startPat, start)) != -1)
        {
            temp = str.indexOf(endPat, start + startPat.length());
            String str1 = str.substring(0, start);
            String str2 = str.substring(temp + endPat.length(), str.length());

            str = str1 + replacer + str2;
            start = start + temp;
        }
        return str;
    }

    public String string_replacer_start_zero(String str, String startPat, String endPat, String replacer)
    {
        int start = 0;
        int temp = 0;
        while((start = str.indexOf(startPat, start)) != -1)
        {
            temp = str.indexOf(endPat, start + startPat.length());
            String str1 = str.substring(0, start);
            String str2 = str.substring(temp + endPat.length(), str.length());

            str = str1 + replacer + str2;
            start = 0;
        }
        return str;
    }

    public String string_extractor(String str, String startPat, String endPat)
    {
        int index = 0;
        int temp = 0;

        index = str.indexOf(startPat, index);
        index = index + startPat.length();
        temp = index;
        index = str.indexOf(endPat, index);
        if(index != -1)
        {
            str = str.substring(temp, index);
        }
        else
        {
            str = "";
        }
        return str;

    }

    public String make_query_from_keywords(String keywords, String tokenSeperator)
    {
        StringBuffer strBuf = new StringBuffer();
        StringTokenizer st1 = new StringTokenizer(keywords, tokenSeperator);
        while(st1.hasMoreElements())
        {
            String strLink = st1.nextToken();
            strBuf.append(strLink);
            strBuf.append("+");
        }
        return strBuf.toString().substring(0, strBuf.length() - 1);
    }

    public String[] generalStringTokenizer(String keywords, String tokenSeperator)
    {
        String[] element = null;
        int n = 0;
        StringTokenizer st1 = new StringTokenizer(keywords, tokenSeperator);
        while(st1.hasMoreElements())
        {
            String strLink = st1.nextToken();
            element[n] = strLink;
            n ++;
        }
        return element;
    }

    public String replace_letter(String input, String tokenSeperator, String replacer)
    {
        StringBuffer strBuf = new StringBuffer();
        StringTokenizer st1 = new StringTokenizer(input, tokenSeperator);
        while(st1.hasMoreElements())
        {
            String strLink = st1.nextToken();
            strBuf.append(strLink);
            strBuf.append(replacer);
        }
        return strBuf.toString().substring(0, strBuf.length() - 1);
    }
}


